package com.zzyl.framework.interceptor.impl;
/*
 *TODO
 *@Autor Adam
 *@Date  2025/7/21  09:39
 *
 */

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

import org.springframework.stereotype.Component;

import javax.swing.*;

import static com.zzyl.common.utils.SecurityUtils.getLoginUser;


@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 添加创建信息
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        strictInsertFill(metaObject, "createTime", Date.class, new Date());
        strictInsertFill(metaObject, "createBy", String.class, String.valueOf(getUserId()));
    }

    /**
     * 添加修改信息
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", new Date(), metaObject);
        LoginUser loginUser = getLoginUser();
        if(loginUser!=null){
            this.setFieldValByName("updateBy", String.valueOf(loginUser.getUserId()), metaObject);
        }
    }

    /**
     * 获取当前操作人ID
     *
     * @return
     */
    public Long getUserId() {
        Long userId = SecurityUtils.getUserId();
        if (userId != 0) {
            return userId;
        } else {
            return 1l;
        }
    }
}
